package com.chenyaowei.happyhub.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wechat")
public class WeChatLoginController {
    @RequestMapping("/loginsuccess")
    String loginSuccess(HttpServletRequest request) throws Exception{
        ObjectMapper mapper=new ObjectMapper();
        Object user=request.getSession().getAttribute("successAuthentication");
        Map<String,Object> map=new HashMap<>();
        map.put("code","ok");
        map.put("user",user);
        String str=mapper.writeValueAsString(map);
        return str;
    }
    @RequestMapping("/loginfail")
    String loginfail(HttpServletRequest request) throws Exception{
        ObjectMapper mapper=new ObjectMapper();
        Map<String,Object> map=new HashMap<>();
        Object message=request.getSession().getAttribute("WeChatLoginErrorMessage");
        map.put("code","error");
        map.put("message",message);
        String str=mapper.writeValueAsString(map);
        return str;
    }
}
